package com.man;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.std.DB;
import com.std.UserMessage;

public class UserMessageCtrl {
	private DB db;
	public UserMessageCtrl(){
		db=new DB();
	}
	public void close() {
		db.close();
	}
	public void insertItem(String username,String title,String content) {
		String sql="insert into user_message_t values(user_message_s.nextval,?,?,?,current_date)";
		Object []params={username,title,content};
		db.execute(sql, params);
	}
	public List<UserMessage> getItems() {
		String sql="select * from user_message_t";
		db.execute(sql, null);
		List<UserMessage> userMessageList=new ArrayList<UserMessage>();
		ResultSet rs=db.getResultset();
		try {
			while(rs.next()){
				UserMessage userMessage=new UserMessage();
				userMessage.setContent(rs.getString("content"));
				userMessage.setSubmitTime(rs.getDate("submit_time"));
				userMessage.setTitle(rs.getString("title"));
				userMessage.setUserMessageId(rs.getInt("user_message_id"));
				userMessage.setUsername(rs.getString("username"));
				userMessageList.add(userMessage);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return userMessageList;
	}
	public UserMessage getItem(int userMessageId) {
		String sql="select * from user_message_t where user_message_id=?";
		Object []params={userMessageId};
		db.execute(sql, params);
		ResultSet rs=db.getResultset();
		try {
			if(rs.next()){
				UserMessage userMessage=new UserMessage();
				userMessage.setContent(rs.getString("content"));
				userMessage.setSubmitTime(rs.getDate("submit_time"));
				userMessage.setTitle(rs.getString("title"));
				userMessage.setUserMessageId(rs.getInt("user_message_id"));
				userMessage.setUsername(rs.getString("username"));
				return userMessage;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	public void deleteItem(int userMessageId) {
		String sql="delete from user_message_t where user_message_id=?";
		Object []params={userMessageId};
		db.execute(sql, params);
	}
}
